<?php
/*
	PHP Mysql 操作
 */
$pw = ''; // 在这里定义访问参数 访问 ?pw=
if(!isset($_GET['pw']) || $_GET['pw'] !== $pw) {
	header("HTTP/1.1 404 Not Found");
	header("Status: 404 Not Found");
	exit;  
}

function my_mysql_connect($connect) {
	// 解析链接参数
	$a = explode(';', trim($connect, ';'));
	foreach($a as $v){
		$vv = explode('=', $v);
		$conn[$vv[0]] = $vv[1];
	}
	if(!isset($conn['host'])) $conn['host'] = '127.0.0.1';
	if(!isset($conn['port'])) $conn['port'] = 3306;
	return $conn;
}
$table = $table_head ='';
$sql = isset($_POST['query']) ? $_POST['query'] : '';
$connect = isset($_POST['connect']) ? $_POST['connect'] : '';
$top = '
		<div class="container" style="margin-top:60px;">
			<form action="" method="POST" class="form-horizontal">
				<div class="form-group">
					<label for="connect" class="col-sm-2 control-label">连接参数：</label>
					<div class="col-sm-10">
						<input name="connect" id="connect" value="' . $connect . '" class="form-control" />
					</div>
				</div>

				<div class="form-group">
					<label for="query" class="col-sm-2 control-label">查询语句：</label>
					<div class="col-sm-10">
						<textarea class="form-control" id="query" placehold="查询语句" name="query">' . $sql . '</textarea>
					</div>
				</div>
				<input type="hidden" name="p" value="o" />
				<div class="form-group">
    				<div class="col-sm-offset-2 col-sm-10">
      					<button type="submit" class="btn btn-default">提交查询</button>
    				</div>
  				</div>
			</form>
		</div>
	';
$error = '';
if(isset($_POST['p'])) {
	if(empty($connect)) $error = '请填写链接参数';
	else if(empty($sql)) $error = '请填写查询语句';
	else {
		$conn_array = my_mysql_connect($connect);
		if(function_exists('mysqli_connect')) {
			$conn = @mysqli_connect($conn_array['host'], $conn_array['username'], $conn_array['password'], $conn_array['database']);
			if (mysqli_connect_errno($conn)) { 
    			$error = "连接 MySQL 失败: " . mysqli_connect_error();
			} else {
				mysqli_query($conn,"set names utf8");
				$query = mysqli_query($conn,$sql);
				if(!$query) {
					$error = "MySQL语句执行出错:\n" . mysqli_error($conn);
				} else {
					$error = "MySQL语句执行成功";
					if(stristr($sql, 'select')) {
					while($row = mysqli_fetch_array($query)){
					if(empty($table_head)) {
						$table_head = '<tr>';
						foreach($row as $key => $val) {
							if(!is_numeric($key)) $table_head .= '<td>' . $key . '</td>';
						}
						$table_head .= '</tr>'."\r\n";
					}
					$table .= '<tr>';
					foreach($row as $keys => $vals) {
						if(!is_numeric($keys)) $table .= '<td>' . $vals . '</td>';
					}
						$table .= '</tr>'."\r\n";
					}
					} else {
						$error .= "\r\n".'受影响的条数：' . mysqli_affected_rows($conn);
					}
				}
			}
		} else if(function_exists('mysql_connect')) {
			$conn = @mysql_connect($conn_array['host'], $conn_array['username'], $conn_array['password']);
			if(!$conn) {
				$error = 'mysql连接失败，请检查连接参数填写是否正确';
			} else {
				$select_db = @mysql_select_db($conn_array['database']);
				if (!$select_db) {
    				$error = "mysql连接失败，请检查连接参数填写是否正确:\n" .  mysql_error();
				} else {
					$res = @mysql_query($sql);
					if (!$res) {
    					$error = "MySQL语句执行出错:\n" . mysql_error();
					} else {
						$error = "MySQL语句执行成功";
						if(stristr($sql, 'select')) {
						while($row = mysql_fetch_array($res)){
							if(empty($table_head)) {
								$table_head = '<tr>';
								foreach($row as $key => $val) {
									if(!is_numeric($key)) $table_head .= '<td>' . $key . '</td>';
								}
								$table_head .= '</tr>'."\r\n";
							}
							$table .= '<tr>';
							foreach($row as $keys => $vals) {
								if(!is_numeric($keys)) $table .= '<td>' . $vals . '</td>';
							}
							$table .= '</tr>'."\r\n";
						}
						} else {
							$error .= "\r\n".'受影响的条数：' . mysql_affected_rows();
						}
					}
				}
			}
		}
	}
}
?>
<!doctype html>
<html>
<head>
	<meta charset="utf-8" />
	<title>sql操作</title>
	<style type="text/css" media="screen">
	</style>
	<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
</head>
<body>
	<?php 
		echo $top;
		if(!empty($error)) {
			echo '<div class="container">
					<div class="alert alert-warning" role="alert">
						<p style="font-size:120%; color:red;">' . $error . '</p>
					</div>
				</div>';
		}
	?>
	<?php if(!empty($table)) {?>
	<div class="container">
	<table class="table table-bordered">
		<tbody>
			<?php echo $table_head; ?>
			<?php echo $table; ?>
		</tbody>
	</table>

	</div>
	<?php } ?>
</body>
</html>